import 'dart:convert';

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hbtrans_flutter/beans/info_bean.dart';
import 'package:hbtrans_flutter/utils/mydio.dart';
import 'package:hbtrans_flutter/view/loading.dart';

import '../const.dart';
import '../routes.dart';

class MessageManager extends StatefulWidget {
  final String pageIndex;

  MessageManager({String pageIndex}) : this.pageIndex = pageIndex;

  @override
  _MessageManagerState createState() =>
      _MessageManagerState(pageIndex: pageIndex);
}

class _MessageManagerState extends State<MessageManager>
    with AutomaticKeepAliveClientMixin {
  String pageIndex;

  var loadingDialog;
  var titles = ['驾培机构', '教学区域', '教练员', '教练车', '安全员', '考核员'];

  var todoImgs = [
    AssetImage('images/icon_jpjg.png'),
    AssetImage('images/icon_jxqy.png'),
    AssetImage('images/icon_jly.png'),
    AssetImage('images/icon_jlc.png'),
    AssetImage('images/icon_aqy.png'),
    AssetImage('images/icon_khy.png'),
  ];
  var points = List();
  int _schCount = 0,
      _coaCount = 0,
      _vehCount = 0,
      _dayCount = 0,
      _monthCount = 0,
      _yearCount = 0;
  int _schDai = 0,
      _schTiregionDai = 0,
      _coaDai = 0,
      _vehDai = 0,
      _exaDai = 0,
      _secDai = 0;

  _showDialog() {
    loadingDialog = LoadingDialog(
      text: '请稍候...',
    );
    showDialog<Null>(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return loadingDialog;
        });
  }

  // Flutter主动发送参数给原生
  static const platform = const MethodChannel("getMethod4Flutter");

  _MessageManagerState({String pageIndex}) : this.pageIndex = pageIndex;

  Future<Null> getMethod4Flutter(String method) async {
    try {
      await platform.invokeMethod(method);
    } on PlatformException {}
  }

  _infomation() {
    MyDio.getInstance().get(
      '/api/infomation',
      (data) {
        InfoBean infoBean = InfoBean.fromJson(data);
        setState(() {
          _schCount = infoBean.schCount;
          _coaCount = infoBean.coaCount;
          _vehCount = infoBean.vehCount;
          _dayCount = infoBean.dayCount;
          _monthCount = infoBean.monthCount;
          _yearCount = infoBean.yearCount;
          _schDai = infoBean.schDai;
          _schTiregionDai = infoBean.schTiregionDai;
          _coaDai = infoBean.coaDai;
          _vehDai = infoBean.vehDai;
          _exaDai = infoBean.exaDai;
          _secDai = infoBean.secDai;
          points = [
            _schDai,
            _schTiregionDai,
            _coaDai,
            _vehDai,
            _exaDai,
            _secDai
          ];
        });
      },
    );
  }

  @override
  void initState() {
    super.initState();
    _infomation();
  }

  Future<Null> _onRefresh() async {
    await _infomation();
    return;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xff23AFDC),
        elevation: 0,
        centerTitle: true,
        title: Text('信息管理'),
      ),
      body: RefreshIndicator(
        onRefresh: _onRefresh,
        child: ListView(
          children: <Widget>[
            Container(
              padding: EdgeInsets.fromLTRB(40, 20, 40, 20),
              color: const Color(0xff23AFDC),
              child: Row(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  InkWell(
                    onTap: () {
//                      intentList(Const.COMPANYS, 0, 0);
                    },
                    child: Column(
                      children: <Widget>[
                        Text(
                          _schCount.toString(),
                          style: TextStyle(fontSize: 14, color: Colors.white),
                        ),
                        SizedBox(
                          height: 5,
                        ),
                        Text(
                          Const.TITLES_UP[0],
                          style: TextStyle(fontSize: 10, color: Colors.white),
                        ),
                      ],
                    ),
                  ),
                  InkWell(
                    onTap: () {
//                      intentList(Const.TEACHERS, 1, 0);
                    },
                    child: Column(
                      children: <Widget>[
                        Text(
                          _coaCount.toString(),
                          style: TextStyle(fontSize: 14, color: Colors.white),
                        ),
                        SizedBox(
                          height: 5,
                        ),
                        Text(
                          Const.TITLES_UP[1],
                          style: TextStyle(fontSize: 10, color: Colors.white),
                        ),
                      ],
                    ),
                  ),
                  GestureDetector(
                    onTap: () {
//                      intentList(Const.CARS, 2, 0);
                    },
                    child: Column(
                      children: <Widget>[
                        Text(
                          _vehCount.toString(),
                          style: TextStyle(fontSize: 14, color: Colors.white),
                        ),
                        SizedBox(
                          height: 5,
                        ),
                        Text(
                          Const.TITLES_UP[2],
                          style: TextStyle(fontSize: 10, color: Colors.white),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Container(
              color: const Color(0xff23AFDC),
              padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
              child: Container(
                  padding: EdgeInsets.fromLTRB(24, 0, 24, 0),
                  color: Colors.white,
                  child: Row(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      GestureDetector(
                        onTap: () {
//                          intentList(Const.STUDENTS1, 3, 0);
                        },
                        child: Column(
                          children: <Widget>[
                            SizedBox(
                              height: 20,
                            ),
                            Text(
                              _dayCount.toString(),
                              style: TextStyle(fontSize: 14),
                            ),
                            SizedBox(
                              height: 5,
                            ),
                            Text(
                              Const.TITLES_UP[3],
                              style: TextStyle(fontSize: 10),
                            ),
                          ],
                        ),
                      ),
                      GestureDetector(
                        onTap: () {
//                          intentList(Const.STUDENTS2, 4, 0);
                        },
                        child: Column(
                          children: <Widget>[
                            SizedBox(
                              height: 20,
                            ),
                            Text(
                              _monthCount.toString(),
                              style: TextStyle(fontSize: 14),
                            ),
                            SizedBox(
                              height: 5,
                            ),
                            Text(
                              Const.TITLES_UP[4],
                              style: TextStyle(fontSize: 10),
                            ),
                          ],
                        ),
                      ),
                      GestureDetector(
                        onTap: () {
//                          intentList(Const.STUDENTS3, 5, 0);
                        },
                        child: Column(
                          children: <Widget>[
                            SizedBox(
                              height: 20,
                            ),
                            Text(
                              _yearCount.toString(),
                              style: TextStyle(fontSize: 14),
                            ),
                            SizedBox(
                              height: 5,
                            ),
                            Text(
                              Const.TITLES_UP[5],
                              style: TextStyle(fontSize: 10),
                            ),
                          ],
                        ),
                      ),
                    ],
                  )),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
              child: Container(
                color: Colors.white,
                child: SizedBox(
                  width: 9999,
                  height: 20,
                ),
              ),
            ),
            SizedBox(height: 10),
            Container(
              padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
              color: Colors.white,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text(
                        "待办事项",
                        style: TextStyle(
                            color: const Color(0xFF333333), fontSize: 16),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Container(
                      margin: EdgeInsets.fromLTRB(37, 20, 37, 20),
                      child: _buildGrid(4, titles.length, 1 / 1)),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildGrid(int i, int count, var retio) {
    return GridView.count(
      // 禁止滚动
      physics: NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      childAspectRatio: retio,
      //长宽比
      //横轴数量 这里的横轴就是x轴 因为方向是垂直的时候 主轴是垂直的
      crossAxisCount: i,
      padding: const EdgeInsets.all(0),
      //主轴间隔
      mainAxisSpacing: 2,
      //横轴间隔
      crossAxisSpacing: 2,
      children: _buildGridTileList(count),
    );
  }

  List<GestureDetector> _buildGridTileList(int count) {
    return List.generate(
      count,
      (int index) => GestureDetector(
          onTap: () {
            var hostTitle = jsonEncode(Utf8Encoder().convert(titles[index]));
            String route =
                '${Routes.pageHost}?hostTitle=$hostTitle&pageType=$index';
            Routes.router.navigateTo(context, route,
                replace: false, transition: TransitionType.fadeIn);
          },
          child: Container(
            height: 52,
            width: 52,
            child: Stack(
              children: <Widget>[
                Center(
                  child: Column(
                    children: <Widget>[
                      ImageIcon(
                        todoImgs[index],
                        size: 52,
                        color: const Color(0xff666666),
                      ),
                      Text(
                        titles[index],
                        style: TextStyle(
                            fontSize: 10, color: const Color(0xff666666)),
                      ),
                    ],
                  ),
                ),
                Positioned(
                    right: 18,
                    top: 5,
                    child: Icon(
                      Icons.brightness_1,
                      size: 12,
                      color: points.length == 0 || points[index] == 0
                          ? Colors.transparent
                          : Colors.red,
                    ))
              ],
            ),
          )),
    );
  }

  intentList(String type, int titleIndex, int page) async {
    String route =
        '${Routes.listPage}?type=$type&titleIndex=$titleIndex&page=$page';
    Routes.router.navigateTo(context, route,
        replace: false, transition: TransitionType.fadeIn);
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
